package exabase.com;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PeerListener extends Thread {
	private static Logger logger = Logger.getLogger(PeerListener.class
			.getName());

	private ServerSocket serverSocket;
	private int numThreads = 2;

	public PeerListener() throws IOException {
		serverSocket = new ServerSocket();
		serverSocket.bind(null);
	}

	public void run() {
		logger.finer(PeerListener.class.getName() + " is running.");
		for (int i = 0; i < numThreads; i++) {
			Thread t = new Thread(new RequestProcessor());
			t.start();
		}

		while (true) {
			try {
				Socket request = serverSocket.accept();
				logger.finer("accept");
				RequestProcessor.processRequest(request);
			} catch (IOException ex) {
				logger.log(Level.WARNING,
						"Soething went wrong when opening a server socket", ex);
			}
		}
	}
}